package com.core.mvc.service;

import java.util.ArrayList;
import java.util.List;

import com.core.IFilter;

class IFilterHandler<T>
{
	private List< IFilter<T> > filters = new ArrayList<>();

	public void addFilter( IFilter<T> filter )
	{
		synchronized ( filters )
		{
			if ( filter != null && !filters.contains( filter ) )
			{
				filters.add( filter );
			}
		}
	}

	/**
	 * 进行过滤
	 *
	 * @param request请求对象
	 * @param response回复对象
	 * @return true 表示有效，false表示无效
	 */
	public boolean doFilter( T value )
	{
		synchronized ( filters )
		{
			boolean ret = true;
			if ( filters != null && filters.size() > 0 )
			{

				for ( IFilter<T> filter : filters )
				{
					ret = filter.doFilter( value );
					if ( !ret )
					{
						break;
					}
				}
			}
			return ret;
		}
	}
}
